<?php
/**
 * 注意：本内容仅限于博也公司内部传阅,禁止外泄以及用于其他的商业目的
 * @author    hebidu<346551990@qq.com>
 * @copyright 2017 www.itboye.com Boye Inc. All rights reserved.
 * @link      http://www.itboye.com/
 * @license   http://www.opensource.org/licenses/mit-license.php MIT License
 * Revision History Version
 ********1.0.0********************
 * file created @ 2017-11-13 15:41
 *********************************
 ********1.0.1********************
 *
 *********************************
 */

namespace app\admin\controller;


use app\src\base\helper\ConfigHelper;
use app\src\base\helper\ValidateHelper;
use app\src\file\logic\UserPictureLogic;
use app\src\sunsun\adt\action\AdtClientAction;
use app\src\sunsun\aph300\action\Aph300ClientAction;
use app\src\sunsun\aq806\action\Aq806ClientAction;
use app\src\sunsun\common\action\DeviceGetVersionAction;
use app\src\sunsun\common\helper\DeviceTypeHelper;
use app\src\sunsun\cp1000\action\Cp1000ClientAction;
use app\src\sunsun\filterVat\action\FilterVatClientAction;
use app\src\sunsun\filterVat\logic\FilterVatDeviceLogic;
use app\src\sunsun\heatingRod\action\HeatingRodClientAction;
use app\src\sunsun\water_pump\action\WaterPumpClientAction;
use sunsun\server\consts\DeviceType;
use think\Controller;
use think\Response;

class DeviceUpdate extends Controller
{
    public function updateFilter()
    {
        set_time_limit(0);
        $isCheck = $this->request->param('is_check', false);
        $ver = "v1.1";
        $result = (new FilterVatDeviceLogic())->query(['ver' => $ver, 'tcp_client_id'=>['neq', '']]);
        $action = (new FilterVatClientAction());
        $url = "http://api.sunsunxiaoli.com/index.php/user_file/download?id=14";
        $len = 51280;
        $list = [];
        if ($result['status']) {
            $list = $result['info']['list'];
        }
        if (count($list) > 0) {
            echo  'start update '. count($list).'<br/>';
            foreach ($list as $vo) {
                if (!$isCheck) {
                    $result = $action->update($vo['did'], $url, $len);
                    echo 'update '.$vo['did'] .  ' __ '. $result['info'].'<br/>';
                    sleep(1);
                } else {
                    echo 'update '.$vo['did'].'<br/>';
                }
            }
        } else {
            echo  'no update';
        }
        return '<br/>over</br>';
    }

    public function index()
    {
        if ($this->request->isPost()) {
            $did = $this->request->post('did', '');
            $url = $this->request->post('url', '');
            $bytes = $this->request->post('bytes', 0);
            $url = 'http://api.sunsunxiaoli.com/index.php/user_file/download?id='.$url;
            $this->updateToLatestFirmware($did, $url, $bytes);
        }
        return $this->fetch('default/'.(request()->controller() . '/' . request()->action()));
    }

    /**
     * 更新到最新设备固件
     * @author hebidu <email:346551990@qq.com>
     * @modify 2017-11-13 14:58:04
     * @param string $did
     * @param string $url
     * @param int $bytes
     */
    public function updateToLatestFirmware($did = '', $url = '', $bytes = 0)
    {

        if (empty($url) || $bytes <= 0) {
            $result = (new DeviceGetVersionAction())->version($did);

            $url = $result['info']['url'];
            $bytes = $result['info']['bytes'];
            if (!ValidateHelper::legalArrayResult($result)) {
                $this->error('不支持该设备类型');
            }
        }
        $action = null;
        $type = DeviceType::getDeviceType($did);

        switch ($type) {
            case DeviceTypeHelper::Did_AQ500:
            case DeviceTypeHelper::Did_AQ806:
                $action = new Aq806ClientAction();
                break;
            case DeviceTypeHelper::Did_APH300:
                $action = new Aph300ClientAction();
                break;
            case DeviceTypeHelper::Did_WaterPump:
                $action = new WaterPumpClientAction();
                break;
            case DeviceTypeHelper::Did_ADT:
                $action = new AdtClientAction();
                break;
            case DeviceTypeHelper::Did_CP1000:
                $action = new Cp1000ClientAction();
                break;
            case DeviceTypeHelper::Did_FilterVat:
                $action = new FilterVatClientAction();
                break;
            case DeviceTypeHelper::Did_HeatingRod:
                $action = new HeatingRodClientAction();
                break;
            default:
                break;
        }
        if ($action) {
            $result = $action->update($did, $url, $bytes);
            if ($result['status']) {
                $this->success('发送成功');
            } else {
                $this->error($result['info']);
            }
        }

        $this->error($result['info']);
    }



    public function upload_file(){

        if($this->request->isPost()){

            if(!isset($_FILES['wxshop'])){
                $this->error("文件对象必须为wxshop");
            }

            $result['info'] = "";
            //2.再上传到自己的服务器，
            //TODO:也可以上传到QINIU上
            /* 返回标准数据 */
            $return  = array('status' => 1, 'info' => '上传成功', 'data' => '');

            $type = 'other';//todo: 上传时设置

            /* 调用文件上传组件上传文件 */
            $Picture = new UserPictureLogic();
            $extInfo = array('uid' => 0, 'show_url' => ConfigHelper::upload_path(),'type'=>$type);
            $file = request()->file('wxshop');
            $info = $Picture->upload(
                $file,
                config('user_picture_upload')
                , $extInfo
            );

            /* 记录图片信息 */
            if ($info) {
                $return['status'] = 1;

                if (is_array($info)) {
                    $return['info'] = $info[0];
                } else {
                    $return['info'] = $info;
                }

                // $return = array_merge($info, $return);
            } else {
                $return['status'] = 0;
                $return['info'] = $Picture->getError();
            }
            /* 返回JSON数据 */
            $this->ajaxReturn($return);
        }

    }

    private function ajaxReturn($data){
        $response =  Response::create($data, "json")->code(200);
        $response->header("X-Powered-By","WWW.ITBOYE.COM")->send();
        exit;
    }
}